package com.sinodata.workflow
{
	import common.SurfaceContainer;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;

	public class WorkFlow
	{
		private static var _workFlow:WorkFlow = null
		public static function getInstance():WorkFlow{if (_workFlow == null) _workFlow = new WorkFlow();return _workFlow;}
		public var c:SurfaceContainer;
		public var tasks:ArrayCollection  = new ArrayCollection();
		
		public function getTask(task_id:String):Task{
			for(var i:int = 0; i < this.tasks.length; i++){
				var t:Task = this.tasks[i] as Task;
				if(t.id == task_id)
					return t;
			}
			return null;
		}
		public function start():void{
			if(this.tasks.length > 0){
				var start:Task = (this.tasks.getItemAt(0) as Task);
				exec(start);
			}
		}
		
		public function exec(task:Task,data:Object=null,direction:int = 0):void{
			c.change(task.clazz,task,data,direction);
		}
				
		public function callback(task:Task,condition:String="0",data:Object=null,direction:int=0):void{
			for(var i:int = 0;i < task.transitions.length;i++){
				var tran:Transition = (task.transitions[i] as Transition);
				if(tran.condition == condition){
					var t:Task = getTask(tran.to_task);
					if(t != null)
						exec(t,data,direction);
					else
						Alert.show("没有找到下一步Task id:" + tran.to_task);
					return;
				}
			}
			Alert.show("没有找到合适的任务可以执行");
		}
		
		private var loader:URLLoader;
		private var externalXML:XML; 
		public function init():void{

			var request:URLRequest = new URLRequest("WorkFlow.xml");
			loader = new URLLoader();
			try {
				loader.load(request);
			}
			catch (error:SecurityError)
			{
				trace("A SecurityError has occurred.");
			}
			loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
			
//			
//			var flashView:Task = new Task();
//			flashView.id = "flashView";
//			flashView.name = "读卡完毕";
//			flashView.clazz = "FlashScreen";
//			flashView.config.title = "请刷IC卡";
//			
//			var tran6:Transition = new Transition();
//			tran6.condition = 0;
//			tran6.name = "读卡完毕";
//			tran6.to_task = "welcome";
//			flashView.transitions.addItem(tran6);
//			this.tasks.addItem(flashView);
//			
//			var welcome:Task = new Task();
//			welcome.id = "welcome";
//			welcome.name = "欢迎使用";
//			welcome.clazz = "LoginView";
//			welcome.config = new Object();
//			welcome.config.info  = new Object();
//			welcome.config.info.text = "欢迎使用票据自助管理";
//			welcome.config.btnIdCard = new Object();
//			welcome.config.btnIdCard.visible=false;
//			welcome.config.btnIcCard = new Object();
//			welcome.config.btnIcCard.visible=true;
//			
//			var tran:Transition = new Transition();
//			tran.condition = 0;
//			tran.name = "读取IC卡";
//			tran.to_task = "iccard";
//			welcome.transitions.addItem(tran);
//			
//			var tran1:Transition = new Transition();
//			tran1.condition = 1;
//			tran1.name = "读取ID卡";
//			tran1.to_task = "idcard";
//			welcome.transitions.addItem(tran1);
//			
//			this.tasks.addItem(welcome);
//			
//			
//			var iccard:Task = new Task();
//			iccard.id = "iccard";
//			iccard.name = "欢迎使用";
//			iccard.clazz = "ReadCard";
//			iccard.config.title = "请刷IC卡";
//			
//			var tran3:Transition = new Transition();
//			tran3.condition = 1;
//			tran3.name = "读取IC卡";
//			tran3.to_task = "sucess";
//			iccard.transitions.addItem(tran3);
//			
//			var tran4:Transition = new Transition();
//			tran4.condition = 0;
//			tran4.name = "读取IC卡失败";
//			tran4.to_task = "error";
//			iccard.transitions.addItem(tran4);
//			
//			this.tasks.addItem(iccard);
//			
//			var sucess:Task = new Task();
//			sucess.id = "sucess";
//			sucess.name = "欢迎使用";
//			sucess.clazz = "SucessView";
//			sucess.config.title = "请刷IC卡";
//			
//			var tran5:Transition = new Transition();
//			tran5.condition = 1;
//			tran5.name = "读卡完毕";
//			tran5.to_task = "welcome";
//			sucess.transitions.addItem(tran5);
//			this.tasks.addItem(sucess);
//			
//			var error:Task = new Task();
//			error.id = "error";
//			error.name = "读卡完毕";
//			error.clazz = "ErrorView";
//			error.config.title = "请刷IC卡";
//			error.transitions.addItem(tran5);
//			
//			error.transitions.addItem(tran);
//			this.tasks.addItem(error);
		}
		
		private function loaderCompleteHandler(event:Event):void {
			try {

				externalXML = new XML(loader.data);
				for(var i:int = 0; i < externalXML.children().length(); i++){
					var task:Task = new Task();
					task.id = externalXML.children()[i].@id;
					task.name = externalXML.children()[i].@name;
					task.clazz = externalXML.children()[i].@clazz;
					var attLen:int=externalXML.children()[i].transition.length();
					for(var j:int=0;j<attLen;j++){
						var transt:Transition = new Transition();
						//属性名称
						transt.condition=externalXML.children()[i].transition[j].@condition;
						transt.name=externalXML.children()[i].transition[j].@name;
						transt.to_task=externalXML.children()[i].transition[j].@to_task;
						task.transitions.addItem(transt);
						
					}
					if(externalXML.children()[i]..config != undefined){
						var configobj:Object = new Object();
						var config:XML = new XML(externalXML.children()[i]..config);
						var attLen1:int=config.children().length();
						for(var m:int=0;m<attLen1;m++){
							//属性名称
							var attName1:String=config.children()[m].name();
							configobj[attName1] = new Object();
							var attLen2:int=config.children()[m].attributes().length();
							for(var n:int=0;n<attLen2;n++){
								//属性名称
								var attName2:String=config.children()[m].attributes()[n].name();
//								trace("attName2:" + attName2);
								//属性值
								var attValue2:String=config.children()[m].@[attName2];
//								trace("attValue2:" + attValue2);
								configobj[attName1][attName2] = attValue2;
							}
							task.config = configobj;
						}
					}
					this.tasks.addItem(task);
				}
				WorkFlow.getInstance().start();
			} catch (e:TypeError) {
				Alert.show("Could not parse the XML file." + e);
			}

		}
		
		
		private function errorHandler(e:IOErrorEvent):void {
			Alert.show("Had problem loading the XML File." + e);
		}
	}
}